package org.example.reader;

import org.apache.avro.file.DataFileReader;
import org.apache.avro.file.FileReader;
import org.apache.avro.generic.GenericDatumReader;
import org.apache.avro.generic.GenericRecord;
import org.apache.avro.io.DatumReader;
import org.apache.avro.file.CodecFactory;

import java.io.File;
import java.io.IOException;

public class DeltaManifestReader {

    static {
        // 使用 CodecFactory 的静态方法注册 Zstandard 编解码器
        CodecFactory.addCodec("zstandard", CodecFactory.zstandardCodec(0, false, false));
    }

    public void readDeltaManifests(String deltaManifestListFileName) throws IOException {
        // 使用Avro库读取manifest文件
        File file = new File(deltaManifestListFileName);
        DatumReader<GenericRecord> datumReader = new GenericDatumReader<>();
        
        try (FileReader<GenericRecord> dataFileReader = DataFileReader.openReader(file, datumReader)) {
            while (dataFileReader.hasNext()) {
                GenericRecord record = dataFileReader.next();
                String recordStr = record.toString()
                        .replaceAll("\\\\u[0-9a-fA-F]{4}", "") // 移除 Unicode 转义序列
                        .replaceAll("[^\\x20-\\x7E]", ""); // 只保留可打印ASCII字符
                System.out.println("Record: " + recordStr);
            }
        }
    }

    public static void main(String[] args) throws IOException {
        // 创建 DeltaManifestReader 实例
        DeltaManifestReader reader = new DeltaManifestReader();

        // 指定manifect文件名
        String deltaManifestFileName = "/tmp/paimon/default.db/t_user/index/index-7a37cae1-05a2-420f-ad60-7c86db6ef542-0";

        // 读取并打印文件内容
         reader.readDeltaManifests(deltaManifestFileName);
    }
}